
package com.tools.cleanmaster.manager;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.widget.RemoteViews;

import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;
import com.tools.cleanmaster.ui.activity.MainActivity;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.StringUtils;

public class CleanerNotificationManager {
    private static boolean DEBUG = FeatureConfig.DEBUG_LOG;

    private static String TAG = "CleanerNotificationManager";

    private static CleanerNotificationManager sInstance = null;

    private NotificationManager mNm;

    private Context mContext;

    private CleanerNotificationManager(Context cxt) {
        mContext = cxt;
        mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public static synchronized CleanerNotificationManager getInance(Context cxt) {
        if (sInstance == null) {
            sInstance = new CleanerNotificationManager(cxt);
        }
        return sInstance;
    }

    public void cancel() {
        mNm.cancel(CleanConstant.PHONE_CLEANER_NOTIFICATION);
    }

    public void sendScanNotification(Context context, int type) {
        sendScanNotification(mContext, type, 0);
    }

    public void sendScanNotification(Context context, int type, long size) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        try {
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
            // 不在最顶层才发通知
            if (!CleanConstant.REAL_PACKAGE_NAME.equals(cn.getPackageName())) {
                GlobalConfigSharedPrefManager.setScanNotificationTime(mContext, System.currentTimeMillis());
                showDUCleanMasterNotification(type, size);
            }
        } catch (Exception e) {
            if (DEBUG) {
                LogHelper.w(TAG, e.getMessage());
            }
        }
    }

    private void showDUCleanMasterNotification(int type, long size) {
        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(),
                com.tools.cleanmaster.R.layout.cleanmaster_notification_layout);
        Intent intent = new Intent(mContext, MainActivity.class);
        if (type == CleanConstant.PHONE_CLEANER_NOTIFICATION_TIME) {
            intent.putExtra(CleanConstant.EXTRA_FROM_CLEAN_MASTER_KEY, CleanConstant.PHONE_CLEANER_FROM_NOTIFICATION_TIME);
            remoteViews.setTextViewText(com.tools.cleanmaster.R.id.title,
                    Html.fromHtml(mContext.getString(com.tools.cleanmaster.R.string.cleanmaster_notification_title)));
            remoteViews.setTextViewText(com.tools.cleanmaster.R.id.message,
                    Html.fromHtml(mContext.getString(com.tools.cleanmaster.R.string.cleanmaster_notification_message)));
        } else if (type == CleanConstant.PHONE_CLEANER_NOTIFICATION_HOME) {
            intent.putExtra(CleanConstant.EXTRA_FROM_CLEAN_MASTER_KEY, CleanConstant.PHONE_CLEANER_FROM_NOTIFICATION_HOME);
            remoteViews.setTextViewText(com.tools.cleanmaster.R.id.title,
                    Html.fromHtml(mContext.getString(com.tools.cleanmaster.R.string.cleanmaster_notification_scan_end_title,
                            StringUtils.getSizeString(size))));
            remoteViews.setTextViewText(com.tools.cleanmaster.R.id.message,
                    Html.fromHtml(mContext.getString(com.tools.cleanmaster.R.string.cleanmaster_notification_message)));
        }
        remoteViews.setTextViewText(com.tools.cleanmaster.R.id.tips_button,
                mContext.getString(com.tools.cleanmaster.R.string.cleanmaster_notification_button));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        remoteViews.setOnClickPendingIntent(com.tools.cleanmaster.R.id.tips_button, pi);
        Notification notification = new Notification();
        notification.icon = com.tools.cleanmaster.R.mipmap.ic_launcher;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.contentView = remoteViews;
        notification.contentIntent = pi;
        //NOTIFICATION 展示上报5级活跃
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(TAG, "弹出通知栏=" + type);
        }
        mNm.notify(CleanConstant.PHONE_CLEANER_NOTIFICATION, notification);
    }

    private void report(String key, String contentKey) {

    }
}
